package io.gitee.zhangbinhub.admin.oauth.controller.inner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.common.api.CommonPath
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.oauth.service.UserService
import io.gitee.zhangbinhub.admin.resource.server.base.BaseResourceServerController
import io.gitee.zhangbinhub.admin.resource.server.vo.UserVo
import io.swagger.v3.oas.annotations.*
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.NotBlank
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
 * @author zhang by 16/01/2019
 * @since JDK 11
 */
@Validated
@RestController
@RequestMapping(CommonPath.innerBasePath)
@Tag(name = "用户信息（内部接口）")
class InnerUserController @Autowired
constructor(
    logAdapter: LogAdapter,
    private val userService: UserService
) : BaseResourceServerController(logAdapter) {
    @Operation(summary = "获取当前用户信息", description = "根据当前登录的用户token，返回详细信息")
    @GetMapping(value = [OauthApi.currUser], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun currUser(@Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication): ResponseEntity<UserVo> =
        userService.getUserInfoByLoginNo(bearerTokenAuthentication.name, true)?.let {
            userService.formatUserVo(it)
        }?.let {
            ResponseEntity.ok(it)
        } ?: throw WebException("找不到用户信息")

    @Operation(summary = "查询用户列表", description = "通过角色编码，查询当前机构下的用户列表")
    @GetMapping(
        value = [OauthApi.userList],
        params = ["!orgLevel", "roleCode"],
        produces = [MediaType.APPLICATION_JSON_VALUE]
    )
    @Throws(WebException::class)
    fun getUserListByCurrOrgAndRole(
        @Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication,
        @Parameter(description = "角色编码", required = true)
        @NotBlank(message = "角色编码不能为空")
        @RequestParam(name = "roleCode") roleCode: String
    ): ResponseEntity<List<UserVo>> =
        ResponseEntity.ok(
            userService.getUserVoListByRelativeOrgAndRole(
                bearerTokenAuthentication.name,
                listOf(0),
                roleCode.split(",")
            )
        )

    @Operation(summary = "查询用户列表", description = "通过相对机构级别和角色编码，查询用户列表")
    @GetMapping(
        value = [OauthApi.userList],
        params = ["orgLevel", "roleCode"],
        produces = [MediaType.APPLICATION_JSON_VALUE]
    )
    @Throws(WebException::class)
    fun getUserListByRelativeOrgAndRole(
        @Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication,
        @Parameter(description = "机构层级", required = true)
        @NotBlank(message = "机构层级不能为空")
        @RequestParam(name = "orgLevel") orgLevel: String,
        @Parameter(description = "角色编码", required = true)
        @NotBlank(message = "角色编码不能为空")
        @RequestParam(name = "roleCode") roleCode: String
    ): ResponseEntity<List<UserVo>> =
        ResponseEntity.ok(
            userService.getUserVoListByRelativeOrgAndRole(
                bearerTokenAuthentication.name,
                orgLevel.split(",").map { item -> item.toInt() },
                roleCode.split(",")
            )
        )
}
